﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class IndexController : BaseController
    {
        public IArticleService _article { get; set; }
        public IBannerService _banner { get; set; }
        public IShopService _shop { get; set; }
        public ILiveService _live { get; set; }
        public IUser_oauth_appService _user_oauth_app { get; set; }
        [ResponseCache(Duration = 600)]
        public IActionResult Index()
        {
            ViewBag.redGoods = _article.GetList(6, "where status=0 and channel_id=2 and is_red=1", "sort_id asc,add_time desc");//热点商品
            ViewBag.newsList = _article.GetList(8, "where status=0 and channel_id=1 and img_url<>''", "sort_id asc,add_time desc");//热点商品
            ViewBag.banner = _banner.GetList(1,5, "where status=0 and img_url<>''", "sort_id asc,add_time desc");
            ViewBag.shop = _shop.GetList(1,6, "where img_url<>''", "sort_id asc,add_time desc");
            ViewBag.live = _live.GetList(1,6, "where img_url<>''", "sort_id asc,add_time desc");
            #region wx
            string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_";
            var app = _user_oauth_app.GetFirst();
            string shareUrl = config.weburl + Request.PathBase + Request.Path + Request.QueryString;
            var acmodel = WXTokenHelper.GetAccessToken(redisName, app, shareUrl);
            ViewBag.appid = app.oauth_app_id;
            ViewBag.ts = acmodel.ts;
            ViewBag.noncestr = acmodel.noncestr;
            ViewBag.jsapi_ticket = acmodel.jsapi_ticket;
            ViewBag.signature = acmodel.signature;
            #endregion
            return View();
        }
        [HttpPost]
        public ActionResult EditGPS(float longitude, float latitude)
        {
            HttpContext.Response.Cookies.Append(CMSKeys.LONGITUDE, longitude.ToString());
            HttpContext.Response.Cookies.Append(CMSKeys.LATITUDE, latitude.ToString());
            return Content("ok");
        }
    }
}